package com.atguigu.tingshu.order.receiver;

import com.alibaba.nacos.api.utils.StringUtils;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.order.service.OrderInfoService;
import jakarta.annotation.PostConstruct;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Executable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

@Component
@Slf4j
public class orderReceiver {
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private OrderInfoService orderInfoService;
    @PostConstruct
    public void orderCanncel(){
        RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque(KafkaConstant.QUEUE_ORDER_CANCEL);

        //创建线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.submit(()->{
            while (true){
                String take = null;
                try {
                    //获取消息
                    take = (String) blockingDeque.take();
                } catch (InterruptedException e) {
                    log.info("监听到延迟关单消息：{}", take);
                    e.printStackTrace();
                }

                //判断关闭订单
                if (StringUtil.isNotEmpty(take)){
                    //调用关单
                    orderInfoService.orderCanncal(take);
                }
            }
        });



    }
}
